﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace PFMMain
{
    class PFMApplicationContext:ApplicationContext
    {
        private System.ComponentModel.IContainer components;
        private NotifyIcon o_NotifyIcon;
        private ContextMenu o_ContextMenu;
        private MenuItem exitMenuItem;
        private MenuItem showMenuItem;
        private Form mainForm;
        private Icon workIcon;
        private Icon bkIcon;
        private Timer trPFM;
        private PFMKernels.PFMCoordinator o_Coordinator;
        
        public PFMApplicationContext()
        {
            InitializeContext();
            InitializePFMSystem();
            DoPFMTask();
        }

        private void DoPFMTask()
        {
            //throw new NotImplementedException();
            try
            {
                trPFM.Start();
            }
            catch (Exception ex)
            {
                trPFM.Stop();
                this.mainForm.Text = ex.Message;
            }
        }

        private void InitializePFMSystem()
        {
            //throw new NotImplementedException();
            CreateTimer();
            SetupTimerEvent();
            CreatePFMCoordinator();
        }

        private void CreatePFMCoordinator()
        {
            //throw new NotImplementedException();
            if (this.o_Coordinator == null)
                this.o_Coordinator = new PFMKernels.PFMCoordinator();
        }

        private void SetupTimerEvent()
        {
           // throw new NotImplementedException();
            this.trPFM.Interval = 60 * 1000;
            this.trPFM.Tick += new EventHandler(trPFM_Tick);
        }

        void trPFM_Tick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            trPFM.Stop();
            try
            {
                ChangeProgramIcon();
                o_Coordinator.DoScheduledTask(DateTime.Now);
            }
            finally
            {
                ChangeProgramIcon();
                trPFM.Start();
            }
        }

        private void ChangeProgramIcon()
        {
            if (this.workIcon == this.o_NotifyIcon.Icon)
            {
                this.o_NotifyIcon.Icon = bkIcon;
            }
            else
            {
                this.bkIcon = this.o_NotifyIcon.Icon;
                this.o_NotifyIcon.Icon= this.workIcon;
            }
            
        }

        private void CreateTimer()
        {
           if(this.trPFM==null)
            this.trPFM = new Timer();
        }
        private void InitializeContext()
        {
            this.components = new System.ComponentModel.Container();
            this.o_NotifyIcon = new NotifyIcon(this.components);
            this.o_ContextMenu = new ContextMenu();
            this.showMenuItem = new MenuItem();
            this.exitMenuItem = new MenuItem();


            this.o_NotifyIcon.ContextMenu = this.o_ContextMenu;
            this.o_NotifyIcon.DoubleClick += new EventHandler(o_NotifyIcon_DoubleClick);
            this.o_NotifyIcon.Icon = new Icon(typeof(PFMApplicationContext), "Resources.AppCLOCK.ICO");
            this.workIcon = new Icon(typeof(PFMApplicationContext), "Resources.Factory.ico");
            this.o_NotifyIcon.Text = DateTime.Now.ToLongDateString();
            this.o_NotifyIcon.Visible = true;
            this.o_ContextMenu.MenuItems.AddRange(new MenuItem[]{showMenuItem,exitMenuItem});

            this.showMenuItem.Index = 0;
            this.showMenuItem.Text = "&显示主窗体";
            this.showMenuItem.DefaultItem = true;
            this.showMenuItem.Click += new EventHandler(showMenuItem_Click);

            this.exitMenuItem.Index = 1;
            this.exitMenuItem.Text = "&结束";
            this.exitMenuItem.Click += new EventHandler(exitMenuItem_Click);


            this.o_NotifyIcon.MouseMove += new MouseEventHandler(o_NotifyIcon_MouseMove);
        }

        void o_NotifyIcon_MouseMove(object sender, MouseEventArgs e)
        {
            //throw new NotImplementedException();
            this.o_NotifyIcon.Text = DateTime.Now.ToLongDateString() + ":" + DateTime.Now.ToLongTimeString();
        }

        void exitMenuItem_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            ExitThread();
        }

        void showMenuItem_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            ShowPFMForm();
        }

        private void ShowPFMForm()
        {
            //throw new NotImplementedException();
            if (mainForm == null)
            {
                this.mainForm = new PFMMainForm();
                this.mainForm.Show();
                this.mainForm.FormClosed += new FormClosedEventHandler(mainForm_FormClosed);
            }
            else
                this.mainForm.Activate();
        }

        void mainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //throw new NotImplementedException();
            this.mainForm = null;
        }

        void o_NotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            ShowPFMForm();
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
                if (components != null)
                    components.Dispose();
            base.Dispose(disposing);
        }
        protected override void ExitThreadCore()
        {
            if (mainForm != null)
                mainForm.Close();
            base.ExitThreadCore();
        }
    }
}
